using System;
using System.Collections.Generic;
using System.Text;
using ElteIk.RoboSoccer.Core.Physics;

namespace ElteIk.RoboSoccer.Core
{
    public class RobotPlayer : Physics.TwoWheelDrivenObject, IPlayer
    {
        #region Lifecycle methods

		internal RobotPlayer(Vector2D center, Vector2D heading, float size)
			: base(center)
        {
			this.size = size;
			this.MotorDistance = 5.0f;
            this.Mass = 0.7f;
			this.Heading = heading.Normalized;
            SetupRobotPoints(size, Heading);
        }

        internal RobotPlayer(float size)
            : this(Vector2D.NullVector, Vector2D.X_Axis, size)
        {
        }
        

		#endregion

		#region IPlayer Members

		private string name;
		public string Name
		{
			get { return name; }
            set { name = value; }
		}

		private int id;
		public int Id
		{
			get { return id; }
		}

		private SoccerTeam team;
		public SoccerTeam Team
		{
			get { return team; }
			set { team = value; }
		}

		#endregion

        #region Public Properties

        private float size;
		/// <summary>
		/// The size (width and height) of the robot.
		/// </summary>
		public float Size
        {
            get { return size; }
            set { size = value; }
		}

		#endregion

        #region Robot construction

        /// <summary>
        /// Constructs the robot heading to the given vector.
        /// TODO: Add support for reading the robot's points from an XML file.
        /// </summary>
        /// <param name="size"></param>
        /// <param name="heading"></param>
        private void SetupRobotPoints(float size, Vector2D heading)
        {
            Vector2D headingPerp = heading.Perpendicular;
            Vector2D startPoint = heading * size / 2 + headingPerp * size / 2;

            Points.Add(startPoint);
            Points.Add(startPoint - heading * size);
            Points.Add(startPoint - heading * size - headingPerp * size);
            Points.Add(startPoint - headingPerp * size);      

			Points.Add(startPoint - headingPerp * (size * 7 / 8));

            //float t = (float)Math.Sqrt((double)(size * size / 16 - size * size / 64));

            float t = size / 5;

			Points.Add(startPoint - heading * t - headingPerp * size * 6 / 8);
			Points.Add(startPoint - heading * t - headingPerp * size * 2 / 8);

    		Points.Add(startPoint - headingPerp * size / 8);
        }

        #endregion

        #region Overridden functions

        public override string ToString()
        {
            return Name;
        }

        #endregion
    }
}
